<template>
  <div class="mod-config">
    <div class="z-querybar">
      <el-select filterable clearable v-model="dataForm.productContainerId" placeholder="请选择污水池" style="width: 250px;">
        <el-option v-for="container in containerList" :key="container.id" :label="container.name" :value="container.id"></el-option>
      </el-select>
      <el-select filterable clearable v-model="dataForm.ctUnitId" placeholder="请选择收运单位" style="width: 250px;">
        <el-option v-for="unit in unitList" :key="unit.id" :label="unit.name" :value="unit.id"></el-option>
      </el-select>
      <!-- <el-select filterable clearable v-model="dataForm.dispatchType" placeholder="请选择调度类型" style="width: 250px;">
        <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value"></el-option>
      </el-select> -->
      <z-search-btn @click="getDataList()"></z-search-btn>
    </div>
    <div class="z-pagecontent">
      <el-table :data="dataList" border stripe v-loading="dataListLoading" style="width: 100%;" :height="maxHeight">
        <el-table-column type="index" header-align="center" align="center" label="序号" width="50">
        </el-table-column>
        <el-table-column prop="name" header-align="center" align="center" label="调度名称" min-width="250">
        </el-table-column>
        <el-table-column prop="dispatchType" header-align="center" align="center" label="调度类型" min-width="80" show-overflow-tooltip>
          <template slot-scope="scope">{{ typeMap[scope.row.dispatchType] || '--' }}</template>
        </el-table-column>
        <el-table-column header-align="center" align="center" label="污水池" min-width="160" show-overflow-tooltip>
          <template slot-scope="scope">{{ containerMap[scope.row.productContainerId + ''] || '--' }}</template>
        </el-table-column>
        <el-table-column header-align="center" align="center" label="收运单位" min-width="160" show-overflow-tooltip>
          <template slot-scope="scope">{{ unitMap[scope.row.ctUnitId + ''] || '--' }}</template>
        </el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" width="100" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="distributeHandle(scope.row)">分配任务</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination> -->
    </div>

    <distribute v-if="distributeVisible" ref="distribute" @refreshDataList="getDataList"
      :containerList="containerList" :carList="carList">
    </distribute>
  </div>
</template>

<script>
import distribute from './distribute'
export default {
  components: { distribute },
  data () {
    const now = new Date()
    const pre = new Date(now.getTime() - 3600000 * 24 * 7)
    return {
      maxHeight: document.documentElement.clientHeight - 220,
      unitList: [],
      unitMap: {},
      containerList: [],
      containerMap: {},
      carList: [],
      carMap: {},
      statusList: [
        { value: '0', label: '待司机确认' },
        { value: '01', label: '司机拒绝了' },
        { value: '1', label: '司机收运中' },
        { value: '2', label: '司机收运完成' }
      ],
      statusMap: {
        '0': '待司机确认',
        '01': '司机拒绝了',
        '1': '司机收运中',
        '2': '司机收运完成'
      },
      typeList: [
        { value: '0', label: '预警触发' },
        { value: '1', label: '手动调度' },
      ],
      typeMap: {
        '0': '预警触发',
        '1': '手动调度',
      },
      dataForm: {
        daterange: [pre, now],
        dispatchType: '',
        productContainerId: '',
        ctUnitId: '',
        status: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: -1,
      totalPage: 0,
      dataListLoading: false,
      distributeVisible: false
    }
  },
  mounted () {
    this.getContainerList()
    this.getCarList()
    this.getUnitList()
    this.getDataList()
  },
  methods: {
    getUnitList() {
      this.$http({
        url: this.$http.adornUrl('/sign/signunit/list'),
        method: 'post',
        data: this.$http.adornData({
          page: 1,
          limit: -1
        })
      }).then(({ data }) => {
        const unitMap = {}
        if (data && data.code === 0) {
          this.unitList = data.page.list
          this.unitList.forEach(element => {
            unitMap[element.id + ''] = element.name
          })
        } else {
          this.unitList = []
        }
        this.unitMap = unitMap
      })
    },
    getContainerList () {
      this.$http({
        url: this.$http.adornUrl('/sign/signcontainer/list'),
        method: 'post',
        data: this.$http.adornData({
          page: 1,
          limit: -1
        })
      }).then(({ data }) => {
        const containerMap = {}
        if (data && data.page.list) {
          this.containerList = data.page.list
          this.containerList.forEach(element => {
            containerMap[element.id + ''] = element.name
          })
        } else {
          this.containerList = []
        }
        this.containerMap = containerMap
      })
    },
    getCarList () {
      this.$http({
        url: this.$http.adornUrl('/sign/signcar/list'),
        method: 'post',
        data: this.$http.adornData({
          page: 1,
          limit: -1
        })
      }).then(({ data }) => {
        const carrMap = {}
        if (data && data.page.list) {
          this.carList = data.page.list
          this.carList.forEach(element => {
            carrMap[element.id + ''] = element.name
          })
        } else {
          this.carList = []
        }
        this.carrMap = carrMap
      })
    },
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sign/signcardispatch/waitmanual/list'),
        method: 'post',
        data: this.$http.adornData({
          // 'page': this.pageIndex,
          // 'limit': this.pageSize,
          'productContainerId': this.dataForm.productContainerId,
          'ctUnitId': this.dataForm.ctUnitId,
          'dispatchType': "0"
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.list
        } else {
          this.dataList = []
        }
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    distributeHandle (distribute) {
      this.distributeVisible = true
      this.$nextTick(() => {
        this.$refs.distribute.init(distribute)
      })
    }
  }
}
</script>